package com.lzq.leetcode.hoot100;

import java.util.*;

import com.lzq.leetcode.hoot100.ds.TreeNode;

public class s94 {

    public List<Integer> inorderTraversal(TreeNode root) {

        List<Integer> ret = new ArrayList<>();
        while (root != null) {
            if (root.left == null) {
                ret.add(root.val);
                root = root.right;
            } else {
                // 找到processer
                TreeNode processer = root.left;
                while (processer.right != null && processer.right != root) {
                    processer = processer.right;
                }
                // 讲processer的right修改为当前节点
                if (processer.right == null) {
                    // 第一次访问
                    processer.right = root;
                    root = root.left;
                } else {
                    // 第二次访问
                    processer.right = null;
                    ret.add(root.val);
                    root = root.right;
                }
            }
        }
        return ret;
    }
}
